#!/bin/sh
#脚本需要在 clickhouse 安装包下进行,比如在 $PACKAGES_DIR
#首先需要先加入用户bpm,在安装过程中将目录改变为用chown 改变为bpm的权限,脚本需要设置 JAVA_HOME,datax,clickhouse,zookeeper,mysql主备,DNS
#7.0.144.113上参数是 ck01 1
#7.0.144.121上参数是 ck02 2
#7.0.144.124上参数是 ck03 3
#7.0.144.124上参数是 ck03 3
#7.0.143.205上参数是 ck04 4
#参数变量
export host_name=ck01
export myid=1
export host_shard=01
###########3
host_ip1=7.0.144.113
host_ip2=7.0.144.121
host_ip3=7.0.144.124
host_ip4=7.0.144.134
###################
export host_ck01=ck01
export host_ck02=ck02
export host_ck03=ck03
export host_ck04=ck04
###################
export PACKAGES_DIR=/data/install
export CK_CONFIG=/data/clickhouse1
#首先设置机器名称
hostnamectl set-hostname $host_name
#安装clickhouse
echo "***install clickhouse***"
sleep 2s
cd $PACKAGES_DIR
rm -rf el7
unzip el7.zip
cd $PACKAGES_DIR/el7
rpm -ivh libicu-50.1.2-15.el7.x86_64.rpm
rpm -ivh libtool-ltdl-2.4.2-22.el7_3.x86_64.rpm
rpm -ivh make-3.82-23.el7.x86_64.rpm
rpm -ivh openssl-1.0.2k-12.el7.x86_64.rpm
rpm -ivh unixODBC-2.3.1-11.el7.x86_64.rpm
rpm -ivh clickhouse-common-static-18.14.10-1.el7.x86_64.rpm
rpm -ivh clickhouse-server-common-18.14.10-1.el7.x86_64.rpm
rpm -ivh clickhouse-server-18.14.10-1.el7.x86_64.rpm
rpm -ivh clickhouse-client-18.14.10-1.el7.x86_64.rpm
rpm -ivh clickhouse-debuginfo-18.14.10-1.el7.x86_64.rpm
rpm -ivh clickhouse-test-18.14.10-1.el7.x86_64.rpm
#创建clickhouse-server的数据目录
rm -rf /data/clickhouse1
mkdir -p /data/clickhouse1/{config,log,pid,datadir,olddatadir,tmp,user_files,format_schemas}
chown -R clickhouse:clickhouse /data/clickhouse1
#关闭防火墙,clickhouse-server的开机自启动
echo "***close firewall,clickhouse-server***"
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
cat /etc/selinux/config
systemctl stop firewalld
systemctl disable firewalld
systemctl stop clickhouse-server
systemctl disable clickhouse-server
chkconfig --list
sleep 2s
#修改/etc/hosts
echo "***update /etc/hosts***"
sed -i '3,$d' /etc/hosts
echo "#clickhouse ip" >> /etc/hosts
echo "$host_ip1 ck01" >> /etc/hosts
echo "$host_ip2 ck02" >> /etc/hosts
echo "$host_ip3 ck03" >> /etc/hosts
echo "$host_ip4 ck04" >> /etc/hosts
echo "#zkcluster ip"  >> /etc/hosts
echo "$host_ip1 zk01" >> /etc/hosts
echo "$host_ip2 zk02" >> /etc/hosts
echo "$host_ip3 zk03" >> /etc/hosts

cat /etc/hosts
sleep 2s
#新加bpm 用户,增加环境变量
echo "*** install bpmuser"
userdel bpm
useradd bpm
passwd  bpm
sed -i '93i\bpm    ALL=(ALL)       ALL' /etc/sudoers
sed -i '13,$d' /home/bpm/.bash_profile
echo 'export JAVA_HOME=/opt/jdk1.8.0_131' >> /home/bpm/.bash_profile
echo 'export PATH=$JAVA_HOME/bin:$PATH' >> /home/bpm/.bash_profile
echo 'export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar' >> /home/bpm/.bash_profile
#拷贝jdk
echo "***install jdk1.8.0_131***"
rm -rf /opt/jdk-8u131-linux-x64.tar.gz
rm -rf /opt/jdk1.8.0_131
rm -rf /opt/zookeeper-3.5.3-beta.tar.gz
rm -rf /opt/zookeeper-3.5.3-beta
cp -r  $PACKAGES_DIR/jdk-8u131-linux-x64.tar.gz /opt/
cp -r  $PACKAGES_DIR/zookeeper-3.5.3-beta.tar.gz /opt/
cd /opt/
tar xvf jdk-8u131-linux-x64.tar.gz
tar xvf zookeeper-3.5.3-beta.tar.gz
chown -R bpm:bpm zookeeper-3.5.3-beta
sleep 2s
#zk的安装,zookeeper的配置
echo "***config zookeeper3.5.3 cluster***"
cd /opt/zookeeper-3.5.3-beta
rm -rf logs
cd /opt/zookeeper-3.5.3-beta/conf
cp -a zoo_sample.cfg zoo.cfg
sed -i 's/dataDir=\/tmp\/zookeeper/dataDir=\/data\/zookeeper\/data/g' zoo.cfg
echo "dataLogDir=/data/zookeeper/log" >> zoo.cfg
echo "server.1=ck01:2888:3888" >> zoo.cfg
echo "server.2=ck02:2888:3888" >> zoo.cfg
echo "server.3=ck03:2888:3888" >> zoo.cfg
cd /opt/zookeeper-3.5.3-beta/bin
sed -i '28i\export JAVA_HOME=/opt/jdk1.8.0_131' zkEnv.sh
#zk的数据目录,先删除,在创建
rm    -rf /data/zookeeper
mkdir -p  /data/zookeeper/{data,log,logs}
cd /opt/zookeeper-3.5.3-beta/
ln -s /data/zookeeper/logs logs
cd  /data/zookeeper/data
touch myid
echo "$myid" >> myid
chown -R bpm:bpm /data/zookeeper
#安装datax
echo "***install datax"
rm -rf /opt/datax.tar.gz
cp -r  $PACKAGES_DIR/datax.tar.gz /opt/
cd /opt
#首先删除以前的安装目录
rm -rf datax
tar xvf datax.tar.gz
chown -R bpm:bpm datax
cd /opt/datax
mkdir -p /data/datax/{log,log_perf}
chown -R bpm:bpm /data/datax
rm -rf /opt/datax/log
rm -rf /opt/datax/log_perf
ln -s  /data/datax/log log
ln -s  /data/datax/log_perf log_perf
#安装anaconda2
echo "***install anaconda2"
cp $PACKAGES_DIR/anaconda2.tar.gz /opt/
cd /opt
rm -rf anaconda2
tar xvf anaconda2.tar.gz
chown -R bpm:bpm anaconda2
#安装自定义clickhouse
cp -a $PACKAGES_DIR/clickhouse.tar.gz /opt/
cd /opt
rm -rf clickhouse
tar xvf clickhouse.tar.gz
sleep 2s
#clickhouse的配置文件
echo "***config clickhouse"
cd $PACKAGES_DIR
rm -rf data
unzip data.zip
cp -a ./data/clickhouse1/config/config.xml   $CK_CONFIG/config/
cp -a ./data/clickhouse1/config/users.xml    $CK_CONFIG/config/
cp -a ./data/clickhouse1/config/metrika.xml  $CK_CONFIG/config/
sed -i 's/<interserver_http_host>ck01-01-1<\/interserver_http_host>/<interserver_http_host>'$host_name'<\/interserver_http_host>/g' $CK_CONFIG/config/config.xml
#修改复制节点名称
sed -i 's/<host>ck01-01-1<\/host>/<host>'$host_ck01'<\/host>/g' $CK_CONFIG/config/metrika.xml
sed -i 's/<host>ck01-01-2<\/host>/<host>'$host_ck02'<\/host>/g' $CK_CONFIG/config/metrika.xml
sed -i 's/<host>ck01-02-1<\/host>/<host>'$host_ck03'<\/host>/g' $CK_CONFIG/config/metrika.xml
sed -i 's/<host>ck01-02-2<\/host>/<host>'$host_ck04'<\/host>/g' $CK_CONFIG/config/metrika.xml
#修改 macros
sed -i 's/<shard>01<\/shard>/<shard>'$host_shard'<\/shard>/g' $CK_CONFIG/config/metrika.xml
sed -i 's/<replica>ck01-01-1<\/replica>/<replica>'$host_name'<\/replica>/g' $CK_CONFIG/config/metrika.xml
#是否需要使用clickhouse用户来启动???
#echo "reboot"
#reboot
#启动再另外写一个脚本
#su - bpm
#/opt/zookeeper-3.5.3-beta/bin/zkServer.sh start
#sudo /opt/clickhouse/bin/clickhouse-server1.sh start
#/opt/anaconda2/bin/python2.7 /opt/datax/bin/datax.py /opt/datax/job/job-stream2clickhouse.json
